/* global Cesium */
import dataCenterBus from 'U/dataCenterBus';
export default function initViewer(container) {
    Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(
        73,
        13,
        135,
        53
    );
    let viewer = new Cesium.Viewer(container, {
        baseLayerPicker: false,
        fullscreenButton: false,
        vrButton: false,
        geocoder: false,
        animation: false,
        homeButton: false,
        infoBox: false,
        sceneModePicker: false,
        selectionIndicator: false,
        timeline: false,
        navigationHelpButton: false,
        navigationInstructionsInitiallyVisible: false,
        creditContainer: document.createElement('div'),
        imageryProvider: false,
        contextOptions: {
            webgl: {
                alpha: true,
                depth: false,
                stencil: true,
                antialias: true,
                premultipliedAlpha: true,
                preserveDrawingBuffer: true,
                failIfMajorPerformanceCaveat: true,
            },
            allowTextureFilterAnisotropic: true,
        },
    });

    let labelEntity = viewer.entities.add({
        position: Cesium.Cartesian3.fromDegrees(0, 0),
        label: {
            text: '提示',
            font: '15px sans-serif',
            pixelOffset: new Cesium.Cartesian2(8, 8), // y大小根据行数和字体大小改变
            horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
            showBackground: true,
            backgroundColor: new Cesium.Color(0.165, 0.165, 0.165, 1.0),
        },
    });
    labelEntity.show = false;

    viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(
        Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
    );
    viewer.scene.fog.enabled = false;
    viewer.scene.globe.showGroundAtmosphere = false;
    window.viewer = viewer;

    let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    handler.setInputAction(function(movement) {
        let pickedObject = viewer.scene.pick(movement.position);
        if (
            Cesium.defined(pickedObject) &&
            Cesium.defined(pickedObject.id) &&
            typeof pickedObject.id.leftClick === 'function'
        ) {
            pickedObject.id.leftClick(pickedObject.id);
        }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    // 鼠标悬浮样式修改
    handler.setInputAction(function(movement) {
        let pickedObject = viewer.scene.pick(movement.endPosition);
        if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id)) {
            let cartesian = viewer.camera.pickEllipsoid(
                movement.endPosition,
                viewer.scene.globe.ellipsoid
            );
            labelEntity.position = cartesian;
            labelEntity.show = true;
            if (pickedObject.id.item && pickedObject.id.item.name) {
                labelEntity.label.show = true;
                labelEntity.label.text = pickedObject.id.item.name;
                $('#ViewerContainer').css('cursor', 'pointer');
            } else if (
                pickedObject.id.properties &&
                pickedObject.id.properties.NAME3
            ) {
                labelEntity.label.show = true;
                let reg = /[\u4e00-\u9fa5]/g;
                let regName3 = String(pickedObject.id.properties.NAME3._value)
                    .match(reg)
                    .join('');
                labelEntity.label.text =
                    pickedObject.id.properties.NAME1._value +
                    pickedObject.id.properties.NAME2._value +
                    ',' +
                    regName3;
                $('#ViewerContainer').css('cursor', 'pointer');
            } else {
                labelEntity.label.show = false;
            }
        } else {
            labelEntity.show = false;
            $('#ViewerContainer').css('cursor', 'default');
        }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    // 监控相机高度
    handler.setInputAction(function(wheelment) {
        let height = viewer.camera.positionCartographic.height;
        height > 1700000
            ? dataCenterBus.$emit('isShowSite', true)
            : dataCenterBus.$emit('isShowSite', false);
    }, Cesium.ScreenSpaceEventType.WHEEL);
}
